if(window.plus){
	plusReady();
}else{
	document.addEventListener('plusready',plusReady,false);
}
function plusReady () {
	
	$(document).ready(function(){
		// back		
		document.addEventListener('UniAppJSBridgeReady', function() {
			// let psgInfo = {
			// 	seatName: '123',
			// 	psgName: 'myname',
			// 	psgGender: 'F',
			// 	dest: 'dest',
			// 	tel: '13800138001'
			// }
			// setData('psgInfo', psgInfo)
			// console.log(psgInfo, 'uniReady')
			// uni.postMessage({
			// 	data: {
			// 		action: psgInfo
			// 	}
			// })
			// uni.navigateTo({
			// 	url: '/pages/index/main'
			// });
			document.querySelector('#btn-back').addEventListener('click', function(evt) {		      
				uni.navigateTo({
					url: '/pages/index/main'
				});
			});
		});
		// back end
		
		UpdateState();
		$('input').focus(function(){
			$('#errormsg').html('');
		});  
    
		setTimeout(()=>{			
			let vid = plus.storage.getItem('vid');
			console.log('vid',vid)
			// let login = getQuery('data')
			if (vid != '') {
			  vid = vid.split('##')
			  let username = vid[0]
			  let password = vid[1]  
			  if (username && password) {
			    loadUserInfo (username, password);        
			  }
			}
		}, 1500)
		
		$('#login').click(function(){
			var username = $('#username').val().trim();
			var password = $('#password').val();
			loadUserInfo (username, password);
		});
		if ($.cookie("remember") == "true") {
			$('#username').val($.cookie("username"));
			$('#password').val($.cookie("password"));
		}
		$("#caseVerteClaire").hide();
	});
  
  function getQuery(name) {
      // 正则：[找寻'&' + 'url参数名字' = '值' + '&']（'&'可以不存在）
      let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
      let r = window.location.search.substr(1).match(reg);
      if(r != null) {
          // 对参数值进行解码
          return unescape(r[2]);
      }
      return null;
  }
	
	function loadUserInfo (username, password) {
		UpdateState();
		if (!isOnLine) {
			plus.nativeUI.alert( "网络错误，请联网后重试！");
		} else {
			$("#caseVerteClaire").show();
			var url = server_url+'/9airweb/login.action?username='+username+'&password='+password+url_tail;
			console.log(url);
			var userInfo = getData("user_data");
			$.ajax({
				dataType: "json",
			    url: url,
			    timeout:10000
			}).done(function(backdata, status){
				$("#caseVerteClaire").hide();
				if (status=='success') {
					if (setData("user_data", backdata)) {
						console.log("数据更新成功！");
						loadPages(username, password, "用户名或密码错误");
					}
				}
			}).fail(function(jqXHR, textStatus){
				$("#caseVerteClaire").hide();
			    if(textStatus == 'timeout') {
			        $('#errormsg').html("服务器异常，请稍后重试！");
			        plus.nativeUI.alert( "服务器异常，请稍后重试！");
			    }
			});
			
		}
		
		function loadPages (username, password, msg) {
			userInfo = getData("user_data");
			if(userInfo.type == "success" && userInfo.msg.username.toLowerCase() == username.toLowerCase() && userInfo.msg.password == password){
				window.location.href='index.html';
				if ($("#remember").attr("checked") == "checked") {
					$.cookie("remember", "true", { expires: 7 }); //存储一个带7天期限的cookie 
					$.cookie("username", username, { expires: 7 }); 
					$.cookie("password", password, { expires: 7 }); 
				} else{
					$.cookie("remember", "false", { expire: -1 }); 
					$.cookie("username", "", { expires: -1 }); 
					$.cookie("password", "", { expires: -1 }); 
				}
			} else{
				plus.nativeUI.alert(msg);
				$('#errormsg').html(msg);
				$.cookie("remember", "false", { expire: -1 }); 
				$.cookie("username", "", { expires: -1 }); 
				$.cookie("password", "", { expires: -1 }); 
				uni.redirectTo({
					url: '/pages/login/login'
				});
				return false;
			}
		}
	}

}